package cn.jhz.learn.blog.service.core.auth.article.impl;

import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.component.ValidationHolder;
import cn.jhz.learn.blog.dao.article.BlogArticleSubfieldAssociativeMapper;
import cn.jhz.learn.blog.dao.article.BlogArticleSubfieldMapper;
import cn.jhz.learn.blog.dao.article.BlogArticleSubfieldSubscribeMapper;
import cn.jhz.learn.blog.dao.file.BlogFileMapper;
import cn.jhz.learn.blog.po.article.BlogArticleSubfield;
import cn.jhz.learn.blog.po.file.BlogFile;
import cn.jhz.learn.blog.service.core.auth.article.ConsoleArticleSubfieldService;
import cn.jhz.learn.blog.util.ObjUtil;
import cn.jhz.learn.blog.vo.article.query.page.ConsolePageView;
import cn.jhz.learn.blog.vo.article.submit.SubfieldParam;
import cn.jhz.learn.blog.vo.article.query.view.SubfieldView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("consoleArticleSubfieldService")
public class ConsoleArticleSubfieldServiceImpl implements ConsoleArticleSubfieldService {

    private final BlogArticleSubfieldMapper subfieldMapper;
    private final BlogArticleSubfieldAssociativeMapper associativeMapper;
    private final BlogFileMapper fileMapper;


    @Autowired
    public ConsoleArticleSubfieldServiceImpl(BlogArticleSubfieldMapper subfieldMapper, BlogArticleSubfieldAssociativeMapper associativeMapper, BlogFileMapper fileMapper) {
        this.subfieldMapper = subfieldMapper;
        this.associativeMapper = associativeMapper;
        this.fileMapper = fileMapper;
    }

    @Transactional
    @Override
    public Integer create(Integer uid, @NotNull SubfieldParam param) {
        BlogArticleSubfield subfield = BlogArticleSubfield.builder()
                .blogId(ValidationHolder.getCurrentUser().getId())
                .fileId(param.getImgUrl().isPresent() ?
                        ((BlogFile)ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_FILE.SIGNATURE_CODE + param.getImgUrl().get())).getId()
                        : 0
                )
                .releaseTime(new Date())
                .remark(param.getRemark())
                .status(GLOBAL.RESOURCE.STATUS.VALID)
                .sticky(GLOBAL.RESOURCE.STICK.IS_NOT)
                .visibility(param.getVisibility())
                .title(param.getTitle())
                .build();
        subfieldMapper.insert(subfield);

        return subfield.getId();
    }

    @Transactional
    @Override
    public void delete(Integer uid, Integer id, Boolean deep) {
        BlogArticleSubfield subfield =
                (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id);
        subfield.setStatus(deep ? GLOBAL.RESOURCE.STATUS.DELETE : GLOBAL.RESOURCE.STATUS.DISABLE);
        subfieldMapper.updateByPrimaryKeySelective(subfield);
        associativeMapper.batchUpdateStatusBySubfieldId(deep ? GLOBAL.RESOURCE.STATUS.DELETE : GLOBAL.RESOURCE.STATUS.DISABLE, id);
    }

    @Transactional
    @Override
    public void recycle(Integer uid, Integer id) {
        BlogArticleSubfield subfield =
                (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id);
        subfield.setStatus(GLOBAL.RESOURCE.STATUS.VALID);
        subfieldMapper.updateByPrimaryKeySelective(subfield);
        associativeMapper.batchUpdateStatusBySubfieldId(GLOBAL.RESOURCE.STATUS.VALID, id);
    }

    @Transactional
    @Override
    public void update(Integer uid, @NotNull SubfieldParam param) {
        BlogArticleSubfield subfield =
                (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID+param.getId().get());
        if(param.getImgUrl().isPresent())
            subfield.setFileId(fileMapper.selectBySignatureCode(Lists.newArrayList(param.getImgUrl().get())).get(0).getId());
        BeanUtils.copyProperties(param, subfield);
        subfieldMapper.updateByPrimaryKey(subfield);
    }

    @Transactional
    @Override
    public void stick(Integer uid, Integer id, Boolean sticky) {
        BlogArticleSubfield subfield =
                (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id);
        subfield.setSticky(sticky ? GLOBAL.RESOURCE.STICK.IS : GLOBAL.RESOURCE.STICK.IS_NOT);
        subfieldMapper.updateByPrimaryKey(subfield);
    }

    @Override
    public Map<String, Object> getDetail(Integer uid, Integer id) {
        Map<String, Object> detail =
                ObjUtil.transBeanMap(ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id));
        detail.put("imgUrl", fileMapper.selectByPrimaryKey((Integer) detail.get("fileId")).getSignatureCode());
        detail.remove("fileId");

        return detail;
    }

    @Override
    public Map<String, Object> getCountAndList(Integer uid, @NotNull ConsolePageView pageView) {
        List<SubfieldView> temps;
        List<Byte> visibility,status;
        Map<String, Object> result = Maps.newHashMap();
        PageInfo<SubfieldView> pageInfo;

        switch(pageView.getStatus().get()){
                case 1:
                    status = Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID);
                    break;
                case 3:
                    status = Lists.newArrayList(GLOBAL.RESOURCE.STATUS.AUDIT);
                    break;
                case 4:
                    status = Lists.newArrayList(GLOBAL.RESOURCE.STATUS.DISABLE);
                    break;
                default:
                    throw new RuntimeException("status参数异常");
        }
        visibility = Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.PRIVATE, GLOBAL.RESOURCE.VISIBILITY.OPEN);

        PageHelper.startPage(pageView.getPage_num(), pageView.getPage_size());
        temps = subfieldMapper.selectByBlogId(ValidationHolder.getCurrentUser().getId(), visibility, status);
        pageInfo = PageInfo.of(temps);


        result.put("page_Info", pageInfo);
        result.put("count", subfieldMapper.countByBlogId(ValidationHolder.getCurrentUser().getId()));

        return result;
    }
}